/**
 *====================================================
* 文件名称: FinanceWriteoffServiceImpl.java
 * 修订记录：
  * No    日期				作者(操作:具体内容)
  * 1.    2016年01月11日	chenxy(创建:创建文件)
  *====================================================
  * 类描述：(说明未实现或其它不应生成javadoc的内容)
  */
package com.yuexunit.finance.project.service;

import java.util.Date;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.yuexunit.finance.common.util.BeanUtils;
import com.yuexunit.finance.project.entity.FinanceWriteoffEntity;
import com.yuexunit.finance.project.entity.lexicon.WriteoffTypeEnum;
import com.yuexunit.inject.annotation.Implements;
import com.yuexunit.page.PageCriteria;
import com.yuexunit.page.PageList;
import com.yuexunit.persist.PersistUtil;

/**
 * @ClassName: FinanceWriteoffServiceImpl
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author chenxy
 * @date 2016年01月11日 16:30:52
 */
@Implements(FinanceWriteoffService.class)
public class FinanceWriteoffServiceImpl implements FinanceWriteoffService {

	@Override
	public Long createFinanceWriteoff(FinanceWriteoffEntity entity) {
		PersistUtil.insert(entity);
		return entity.getWriteoffId();
	}

	@Override
	public void updateFinanceWriteoff(FinanceWriteoffEntity entity) {
		FinanceWriteoffEntity persist = inquireFinanceWriteoff(entity.getWriteoffId());
		BeanUtils.copyProperties(entity, persist);
		PersistUtil.update(persist);
	}
	
	@Override
	public void deleteFinanceWriteoff(Long writeoffId) {
		PersistUtil.delete(FinanceWriteoffEntity.class, writeoffId);
	}

	@Override
	public void renovateFinanceWriteoff(FinanceWriteoffEntity persist) {
		PersistUtil.update(persist);
	}

	@Override
	public FinanceWriteoffEntity inquireFinanceWriteoff(Long writeoffId) {
		return PersistUtil.get(FinanceWriteoffEntity.class, writeoffId);
	}

	@Override
	public PageList<FinanceWriteoffEntity> inquireFinanceWriteoffPageList(Long disbursementTenantId, Long receiptTenantId, Long operatorTenantId, WriteoffTypeEnum writeoffType, String writeoffNumber, Date beginDateTime, Date endDateTime,
			String receiptName, String disbursementName, Long accountInfoId, PageCriteria page) {
		DetachedCriteria criteria = DetachedCriteria.forClass(FinanceWriteoffEntity.class);
		criteria.add(Restrictions.eq("operatorTenantId", operatorTenantId));
		if (disbursementTenantId != null) {
			criteria.add(Restrictions.eq("disbursementTenantId", disbursementTenantId));
		}
		if (receiptTenantId != null) {
			criteria.add(Restrictions.eq("receiptTenantId", receiptTenantId));
		}
		if (StringUtils.isNotBlank(writeoffNumber)) {
			criteria.add(Restrictions.like("writeoffNumber", "%" + writeoffNumber + "%"));
		}
		if (writeoffType != null) {
			criteria.add(Restrictions.eq("writeoffType", writeoffType));
		}
		if (beginDateTime != null) {
			criteria.add(Restrictions.ge("writeoffDateTime", beginDateTime));
		}
		if (endDateTime != null) {
			criteria.add(Restrictions.le("writeoffDateTime", endDateTime));
		}
		if (receiptName != null) {
			criteria.add(Restrictions.like("receiptName", "%" + receiptName + "%"));
		}
		if (disbursementName != null) {
			criteria.add(Restrictions.like("disbursementName", "%" + disbursementName + "%"));
		}
		if (accountInfoId != null) {
			criteria.add(Restrictions.eq("accountInfoId", accountInfoId));
		}
		criteria.addOrder(Order.asc("operateDateTime"));
		return PersistUtil.get(criteria, page);
	}
}
